﻿#pragma execution_character_set("utf-8")

#include "frmmain.h"
#include "ui_frmmain.h"
#include "iconhelper.h"
#include "quihelper.h"
#include "linkWidget.h"
#include "flowlayout.h"


#include <QDebug>

frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
{
    QString iniPath =  QCoreApplication::applicationDirPath()+"/config.ini";
    setConfig = new QSettings(iniPath,QSettings::IniFormat);//初始化配置变量

    ui->setupUi(this);
    this->initForm();
    this->initStyle();
    this->initLeftMain();
    this->initLeftConfig();
//增加连接监控标志牌
    FlowLayout *flowLayout = new FlowLayout;
    //动态添加link显示控件
    linkWidget *lw = new linkWidget();

    flowLayout->addWidget(lw);

    ui->scrollAreaMoniterLink->setLayout(flowLayout);
// 设置表格
    this->setStyleSheet("QTableWidget::item{padding:0px;}");
    //设置宽度自适应
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

}

frmMain::~frmMain()
{
    delete setConfig;//删除变量
    delete ui;
}

bool frmMain::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->widgetTitle) {
        if (event->type() == QEvent::MouseButtonDblClick) {
            on_btnMenu_Max_clicked();
            //设置最大化
        }
    }
    return QWidget::eventFilter(watched, event);
}

void frmMain::getQssColor(const QString &qss, const QString &flag, QString &color)
{
    int index = qss.indexOf(flag);
    if (index >= 0) {
        color = qss.mid(index + flag.length(), 7);
    }
    //qDebug() << TIMEMS << flag << color;
}

void frmMain::getQssColor(const QString &qss, QString &textColor, QString &panelColor,
                          QString &borderColor, QString &normalColorStart, QString &normalColorEnd,
                          QString &darkColorStart, QString &darkColorEnd, QString &highColor)
{
    getQssColor(qss, "TextColor:", textColor);
    getQssColor(qss, "PanelColor:", panelColor);
    getQssColor(qss, "BorderColor:", borderColor);
    getQssColor(qss, "NormalColorStart:", normalColorStart);
    getQssColor(qss, "NormalColorEnd:", normalColorEnd);
    getQssColor(qss, "DarkColorStart:", darkColorStart);
    getQssColor(qss, "DarkColorEnd:", darkColorEnd);
    getQssColor(qss, "HighColor:", highColor);
}

void frmMain::initForm()
{
    //设置无边框
    QUIHelper::setFramelessForm(this);
    //设置图标
    IconHelper::setIcon(ui->labIco, 0xf285, 30);
    IconHelper::setIcon(ui->btnMenu_Min, 0xf068);
    IconHelper::setIcon(ui->btnMenu_Max, 0xf067);
    IconHelper::setIcon(ui->btnMenu_Close, 0xf00d);

    //ui->widgetMenu->setVisible(false);
    ui->widgetTitle->setProperty("form", "title");
    //关联事件过滤器用于双击放大
    ui->widgetTitle->installEventFilter(this);
    ui->widgetTop->setProperty("nav", "top");

    QFont font;
    font.setPixelSize(25);
    ui->labTitle->setFont(font);
    ui->labTitle->setText("文件加速传输平台");
    this->setWindowTitle(ui->labTitle->text());

    ui->stackedWidget->setStyleSheet("QLabel{font:60px;}");

    QSize icoSize(32, 32);
    int icoWidth = 85;

    //设置顶部导航按钮
    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setIconSize(icoSize);
        btn->setMinimumWidth(icoWidth);
        btn->setCheckable(true);
        //设置按钮信号和槽
        connect(btn, SIGNAL(clicked()), this, SLOT(buttonClick()));
    }

    ui->btnMain->click();

    ui->widgetLeftMain->setProperty("flag", "left");
    ui->widgetLeftConfig->setProperty("flag", "left");
    ui->page1->setStyleSheet(QString("QWidget[flag=\"left\"] QAbstractButton{min-height:%1px;max-height:%1px;}").arg(60));
    ui->page2->setStyleSheet(QString("QWidget[flag=\"left\"] QAbstractButton{min-height:%1px;max-height:%1px;}").arg(25));
    //初始化配置文件内容
    this->initConfig();
}

void frmMain::initStyle()
{
    loadStyle(":/qss/lightblue.css");
}

//初始化配置文件信息
void frmMain::initConfig()
{

    setConfig->setValue("sendCount",1);
    setConfig->beginGroup("sendTask");
    setConfig->setValue("sendTask1name","www");
    setConfig->endGroup();

    struct Login {
          QString userName;
          QString password;
      };
      QList<Login> logins;
        logins.append({"11","22"});
        logins.append({"22","22"});
        logins.append({"33","22"});
        logins.append({"44","22"});
      setConfig->beginWriteArray("logins");
      for (int i = 0; i < logins.size(); ++i) {
          setConfig->setArrayIndex(i);
          setConfig->setValue("userName", logins.at(i).userName);
          setConfig->setValue("password", logins.at(i).password);
      }
      setConfig->endArray();
      setConfig->beginWriteArray("logins");

          setConfig->setArrayIndex(2);
          setConfig->setValue("userName", "2->**");
          setConfig->setValue("password", "2->##");

      setConfig->endArray();
    qDebug()<<"读取config:"<<setConfig->value("sendCount").toString();
}
void frmMain::loadStyle(const QString &qssFile)
{
    //开启计时
    QElapsedTimer time;
    time.start();

    //加载样式表
    QString qss;
    QFile file(qssFile);
    if (file.open(QFile::ReadOnly)) {
        //用QTextStream读取样式文件不用区分文件编码 带bom也行
        QStringList list;
        QTextStream in(&file);
        //in.setCodec("utf-8");
        while (!in.atEnd()) {
            QString line;
            in >> line;
            list << line;
        }

        file.close();
        qss = list.join("\n");
        QString paletteColor = qss.mid(20, 7);
        qApp->setPalette(QPalette(paletteColor));
        //用时主要在下面这句
        qApp->setStyleSheet(qss);

        //先从样式表中取出对应的颜色
        QString textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor;
        getQssColor(qss, textColor, panelColor, borderColor, normalColorStart, normalColorEnd, darkColorStart, darkColorEnd, highColor);

        //将对应颜色设置到控件
        this->borderColor = highColor;
        this->normalBgColor = normalColorStart;
        this->darkBgColor = panelColor;
        this->normalTextColor = textColor;
        this->darkTextColor = normalTextColor;
        this->initLeftMain();
        this->initLeftConfig();

    }

    qDebug() << "用时:" << time.elapsed();
}

void frmMain::buttonClick()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QString name = b->text();

    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn == b);
    }

    if (name == "主界面") {
        ui->stackedWidget->setCurrentIndex(0);
    } else if (name == "系统设置") {
        ui->stackedWidget->setCurrentIndex(1);
    } else if (name == "日志查询") {
        ui->stackedWidget->setCurrentIndex(2);
    } else if (name == "启动运行") {
        ui->stackedWidget->setCurrentIndex(3);
    } else if (name == "停止运行") {
        //exit(0);
        qDebug()<<"停止"<<"运行";
    }
}

void frmMain::initLeftMain()
{
    iconsMain << 0xf0ec << 0xf15b << 0xf247;//将图标int序列号 放到列表中
    btnsMain << ui->tbtnMain1 << ui->tbtnMain2 << ui->tbtnMain3;//将按钮放在按钮列表中

    int count = btnsMain.count();
    for (int i = 0; i < count; ++i) {
        QToolButton *btn = (QToolButton *)btnsMain.at(i);
        btn->setCheckable(true);
        btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        connect(btn, SIGNAL(clicked(bool)), this, SLOT(leftMainClick()));
    }

    IconHelper::StyleColor styleColor;
    styleColor.position = "left";
    styleColor.iconSize = 18;
    styleColor.iconWidth = 35;
    styleColor.iconHeight = 25;
    styleColor.borderWidth = 4;
    styleColor.borderColor = borderColor;
    styleColor.setColor(normalBgColor, normalTextColor, darkBgColor, darkTextColor);
    IconHelper::setStyle(ui->widgetLeftMain, btnsMain, iconsMain, styleColor);
    ui->tbtnMain1->click();
}

void frmMain::initLeftConfig()
{
    //<< 0xf036 << 0xf249 << 0xf055 << 0xf05a
    iconsConfig << 0xf031  << 0xf249;
    btnsConfig << ui->tbtnConfig1 << ui->tbtnConfig2 ;

    int count = btnsConfig.count();
    for (int i = 0; i < count; ++i) {
        QToolButton *btn = (QToolButton *)btnsConfig.at(i);
        btn->setCheckable(true);
        btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        connect(btn, SIGNAL(clicked(bool)), this, SLOT(leftConfigClick()));
    }

    IconHelper::StyleColor styleColor;
    styleColor.position = "left";
    styleColor.iconSize = 16;
    styleColor.iconWidth = 20;
    styleColor.iconHeight = 20;
    styleColor.borderWidth = 3;
    styleColor.borderColor = borderColor;
    styleColor.setColor(normalBgColor, normalTextColor, darkBgColor, darkTextColor);
    IconHelper::setStyle(ui->widgetLeftConfig, btnsConfig, iconsConfig, styleColor);
    ui->tbtnConfig1->click();
}

void frmMain::leftMainClick()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QString name = b->text();

    int count = btnsMain.count();
    for (int i = 0; i < count; ++i) {
        QAbstractButton *btn = btnsMain.at(i);
        btn->setChecked(btn == b);//设置选中状态
    }
    if(name == "链路监控"){
        ui->stackedWidgetMain->setCurrentIndex(0);
    }else if (name == "文件监控") {
        ui->stackedWidgetMain->setCurrentIndex(1);
    }else if (name == "系统监控") {
        ui->stackedWidgetMain->setCurrentIndex(2);
    }

    //ui->lab1->setText(name);
}

void frmMain::leftConfigClick()
{
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();

    int count = btnsConfig.count();
    for (int i = 0; i < count; ++i) {
        QAbstractButton *btn = btnsConfig.at(i);
        btn->setChecked(btn == b);
    }
    if(name == "任务设置"){
        ui->stackedWidgetSet->setCurrentIndex(0);
    }else if (name == "系统设置") {
        ui->stackedWidgetSet->setCurrentIndex(1);
    }

    //ui->lab2->setText(name);
}

void frmMain::on_btnMenu_Min_clicked()
{
    showMinimized();
}

void frmMain::on_btnMenu_Max_clicked()
{
    static bool max = false;
    static QRect location = this->geometry();

    if (max) {
        this->setGeometry(location);
    } else {
        location = this->geometry();
        this->setGeometry(QUIHelper::getScreenRect());
    }

    this->setProperty("canMove", max);
    max = !max;
}

void frmMain::on_btnMenu_Close_clicked()
{
    close();
}


void frmMain::on_pushButton_blue_clicked()
{
    loadStyle(":/qss/lightblue.css");
}

void frmMain::on_pushButton_gray_clicked()
{
    loadStyle(":/qss/flatgray.css");
}

void frmMain::on_pushButton_black_clicked()
{
    loadStyle(":/qss/blacksoft.css");
}
